home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / Compose.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-27  |  6.9 KB  |  210 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmCompose 
  4.    Caption         =   "Compose []"
  5.    ClientHeight    =   6825
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   8610
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   6825
  11.    ScaleWidth      =   8610
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog dlgOpenFile 
  14.       Left            =   0
  15.       Top             =   840
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.PictureBox picBackground 
  21.       AutoSize        =   -1  'True
  22.       Height          =   3360
  23.       Left            =   120
  24.       Picture         =   "Compose.frx":0000
  25.       ScaleHeight     =   220
  26.       ScaleMode       =   3  'Pixel
  27.       ScaleWidth      =   274
  28.       TabIndex        =   3
  29.       Top             =   0
  30.       Width           =   4170
  31.    End
  32.    Begin VB.PictureBox picForeground 
  33.       AutoSize        =   -1  'True
  34.       Height          =   3360
  35.       Left            =   4320
  36.       Picture         =   "Compose.frx":2C462
  37.       ScaleHeight     =   220
  38.       ScaleMode       =   3  'Pixel
  39.       ScaleWidth      =   274
  40.       TabIndex        =   2
  41.       Top             =   0
  42.       Width           =   4170
  43.    End
  44.    Begin VB.PictureBox picResult 
  45.       Height          =   3360
  46.       Left            =   4320
  47.       ScaleHeight     =   220
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   274
  50.       TabIndex        =   1
  51.       Top             =   3360
  52.       Width           =   4170
  53.    End
  54.    Begin VB.PictureBox picMask 
  55.       Height          =   3360
  56.       Left            =   120
  57.       ScaleHeight     =   220
  58.       ScaleMode       =   3  'Pixel
  59.       ScaleWidth      =   274
  60.       TabIndex        =   0
  61.       Top             =   3360
  62.       Width           =   4170
  63.    End
  64.    Begin VB.Menu mnuFile 
  65.       Caption         =   "&File"
  66.       Begin VB.Menu mnuFileSaveAs 
  67.          Caption         =   "Save &As..."
  68.          Shortcut        =   ^A
  69.       End
  70.    End
  71. Attribute VB_Name = "frmCompose"
  72. Attribute VB_GlobalNameSpace = False
  73. Attribute VB_Creatable = False
  74. Attribute VB_PredeclaredId = True
  75. Attribute VB_Exposed = False
  76. Option Explicit
  77. ' Compose the images.
  78. Private Sub ComposeImages()
  79.     picResult.PaintPicture picBackground.Picture, 0, 0
  80.     picResult.PaintPicture picMask.Picture, _
  81.         0, 0, , , , , , , vbMergePaint
  82.     picResult.PaintPicture picForeground.Picture, _
  83.         0, 0, , , , , , , vbSrcAnd
  84.     ' Make the changes permanent.
  85.     picResult.Picture = picResult.Image
  86. End Sub
  87. ' Make a mask from the foreground picture.
  88. Private Sub MakeMask()
  89. Dim pixels() As RGBTriplet
  90. Dim mask_pixels() As RGBTriplet
  91. Dim bits_per_pixel As Integer
  92. Dim transparent_r As Byte
  93. Dim transparent_g As Byte
  94. Dim transparent_b As Byte
  95. Dim X As Integer
  96. Dim Y As Integer
  97.     ' Get the pixels from the foreground image.
  98.     GetBitmapPixels picForeground, pixels, bits_per_pixel
  99.     ' See what the upper left pixel's color is.
  100.     ' We will convert this value into white and other
  101.     ' values into black.
  102.     With pixels(0, 0)
  103.         transparent_r = .rgbRed
  104.         transparent_g = .rgbGreen
  105.         transparent_b = .rgbBlue
  106.     End With
  107.     ' Allocate the mask pixels.
  108.     ReDim mask_pixels( _
  109.         LBound(pixels, 1) To UBound(pixels, 1), _
  110.         LBound(pixels, 2) To UBound(pixels, 2))
  111.     ' Set the pixel color values.
  112.     For Y = 0 To picForeground.ScaleHeight - 1
  113.         For X = 0 To picForeground.ScaleWidth - 1
  114.             With pixels(X, Y)
  115.                 If (.rgbRed = transparent_r) And _
  116.                    (.rgbGreen = transparent_g) And _
  117.                    (.rgbBlue = transparent_b) _
  118.                 Then
  119.                     ' Set the foreground pixel to white.
  120.                     .rgbRed = 255
  121.                     .rgbGreen = 255
  122.                     .rgbBlue = 255
  123.                     ' Make the mask pixel white, too.
  124.                     mask_pixels(X, Y) = pixels(X, Y)
  125.                 Else
  126.                     ' Set the mask pixel to black.
  127.                     mask_pixels(X, Y).rgbRed = 0
  128.                     mask_pixels(X, Y).rgbGreen = 0
  129.                     mask_pixels(X, Y).rgbBlue = 0
  130.                     ' Leave the foreground pixel alone.
  131.                 End If
  132.             End With
  133.         Next X
  134.     Next Y
  135.     ' Set picForeground's pixels.
  136.     SetBitmapPixels picForeground, bits_per_pixel, pixels
  137.     picForeground.Picture = picForeground.Image
  138.     ' Set picMask's pixels.
  139.     SetBitmapPixels picMask, bits_per_pixel, mask_pixels
  140.     picMask.Picture = picMask.Image
  141. End Sub
  142. ' Start in the current directory.
  143. Private Sub Form_Load()
  144. Dim ctl As Control
  145.     For Each ctl In Controls
  146.         If TypeOf ctl Is PictureBox Then
  147.             ctl.ScaleMode = vbPixels
  148.             ctl.AutoRedraw = True
  149.         End If
  150.     Next ctl
  151.     picBackground.AutoSize = True
  152.     picForeground.AutoSize = True
  153.     dlgOpenFile.CancelError = True
  154.     dlgOpenFile.InitDir = App.Path
  155.     dlgOpenFile.Filter = _
  156.         "Bitmaps (*.bmp)|*.bmp|" & _
  157.         "GIFs (*.gif)|*.gif|" & _
  158.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  159.         "Icons (*.ico)|*.ico|" & _
  160.         "Cursors (*.cur)|*.cur|" & _
  161.         "Run-Length Encoded (*.rle)|*.rle|" & _
  162.         "Metafiles (*.wmf)|*.wmf|" & _
  163.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  164.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  165.         "All Files (*.*)|*.*"
  166.     ' Make the form appear.
  167.     Show
  168.     Screen.MousePointer = vbHourglass
  169.     DoEvents
  170.     ' Make the foreground image's mask.
  171.     MakeMask
  172.     DoEvents
  173.     ' Compose the images.
  174.     ComposeImages
  175.     Screen.MousePointer = vbDefault
  176. End Sub
  177. ' Save the transformed image.
  178. Private Sub mnuFileSaveAs_Click()
  179. Dim file_name As String
  180.     ' Let the user select a file.
  181.     On Error Resume Next
  182.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  183.     dlgOpenFile.ShowSave
  184.     If Err.Number = cdlCancel Then
  185.         Exit Sub
  186.     ElseIf Err.Number <> 0 Then
  187.         Beep
  188.         MsgBox "Error selecting file.", , vbExclamation
  189.         Exit Sub
  190.     End If
  191.     On Error GoTo 0
  192.     Screen.MousePointer = vbHourglass
  193.     DoEvents
  194.     file_name = Trim$(dlgOpenFile.FileName)
  195.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  196.         - Len(dlgOpenFile.FileTitle) - 1)
  197.     Caption = "Compose [" & dlgOpenFile.FileTitle & "]"
  198.     ' Save the transformed image into the file.
  199.     On Error GoTo SaveError
  200.     SavePicture picResult.Picture, file_name
  201.     On Error GoTo 0
  202.     Screen.MousePointer = vbDefault
  203.     Exit Sub
  204. SaveError:
  205.     Screen.MousePointer = vbDefault
  206.     MsgBox "Error " & Format$(Err.Number) & _
  207.         " saving file '" & file_name & "'" & vbCrLf & _
  208.         Err.Description
  209. End Sub
  210.